package com.kaikai.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaikai.edu.entity.EduChapter;
import com.kaikai.edu.entity.EduVideo;
import com.kaikai.edu.entity.chapter.ChapterVo;
import com.kaikai.edu.entity.chapter.VideoVo;
import com.kaikai.edu.mapper.EduChapterMapper;
import com.kaikai.edu.mapper.EduVideoMapper;
import com.kaikai.edu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaikai.servicebase.exceptionhandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-14
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoMapper videoMapper;

    @Override
    public List<ChapterVo> getChapterVideo(String courseId) {
        //1.根据id查询所有章节
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id", courseId);
        List<EduChapter> chapterList = this.list(chapterWrapper);
        //2.查询所有小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        List<EduVideo> eduVideoList = videoMapper.selectList(videoWrapper);

        //用于返回的最终集合
        List<ChapterVo> chapterVoList = new ArrayList<>();

        //3.遍历所有章节进行封装

            for(EduChapter chapter:chapterList){
                ChapterVo chapterVo=new ChapterVo();
                BeanUtils.copyProperties(chapter,chapterVo);

                //用于封装每个章节中所有的小节videoVo数据
                List<VideoVo> videoVoList=new ArrayList<>();

                //4.遍历所有小节,封装List<VideoVo>
                for(EduVideo video:eduVideoList){
                    if(chapter.getId().equals(video.getChapterId())){
                        VideoVo videoVo=new VideoVo();
                        BeanUtils.copyProperties(video,videoVo);
                        videoVoList.add(videoVo);
                    }
                }
                chapterVo.setVideoList(videoVoList);

                chapterVoList.add(chapterVo);
            }
        return chapterVoList;
    }

    /**
     * 删除章节
     * 如果下面有小节,则不能删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteChapter(String id) {

        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        Integer count = videoMapper.selectCount(wrapper);
        if(count>0){
            //查到了有小节
            throw new MyException(20001,"请先删除所有小节后再删除");
        }
        else{
            boolean b = this.removeById(id);
            return  b;
        }

    }
    //通过课程id删除章节
    @Override
    public void deleteChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        this.remove(wrapper);
    }


}
